import 'package:flutter/material.dart';

class CustomMultiChildLayoutTest extends StatefulWidget {
  @override
  _CustomMultiChildLayoutTestState createState() =>
      _CustomMultiChildLayoutTestState();
}

class _CustomMultiChildLayoutTestState extends State<CustomMultiChildLayoutTest>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('CustomMultiChildLayout')),
      body: Container(
        color: Colors.pink[100],
        child: CustomMultiChildLayout(
          delegate: MyDelegate(4),
          children: [
            LayoutId(
                id: 1,
                child: Container(
                  width: 30,
                  height: 50,
                  color: Colors.purple,
                  child: Column(
                    children: [Text('这是list')],
                  ),
                )),
            LayoutId(id: 2, child: FlutterLogo()),
            LayoutId(id: 'underline', child: Container(color: Colors.green)),
            LayoutId(id: 'text', child: Text('hello')),
          ],
        ),
      ),
    );
  }
}

class MyDelegate extends MultiChildLayoutDelegate {
  final double thickness;

  MyDelegate(this.thickness);
  @override
  void performLayout(Size size) {
    print(size);
    Size size1, size2;
    if (hasChild(1)) {
      size1 = layoutChild(1, BoxConstraints.loose(size));
      positionChild(1, Offset(0, 0));
    }
    if (hasChild(2)) {
      size2 = layoutChild(
          2,
          BoxConstraints(
            minWidth: 100,
            minHeight: 100,
            maxWidth: 100,
            maxHeight: 100,
          ));
      positionChild(2, Offset(size1.width, size1.height));
    }
    final sizeText = layoutChild('text', BoxConstraints.loose(size));
    positionChild(
        'text', Offset(size2.width + size1.width, size2.height + size1.height));

    final sizeUnderLine = layoutChild(
        'underline',
        BoxConstraints(
            maxHeight: thickness,
            maxWidth: sizeText.width,
            minHeight: thickness,
            minWidth: sizeText.width));
    positionChild(
        'underline',
        Offset(size2.width + size1.width,
            size2.height + size1.height + sizeText.height));
  }

  @override
  Size getSize(BoxConstraints constraints) {
    // 默认 constraints.biggest
    return Size(300, 300);
  }

  @override
  bool shouldRelayout(_) => true;
}
